//
//  ByPayBombView.m
//  caipiao_ios
//
//  Created by mc on 2018/7/18.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByPayBombView.h"



@implementation ByPayBombView

- (id)init{
    self = [super init];
    if (self) {
        [self initViews];
    }
    return self;
}



- (void)initViews{
    // 设置弹出方式和位置
    self.positionMode = ZKBombPositionModeBottom;
    self.popMode = ZKBombPopModeBottom;
    
    // 自定义视图
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 375)];
    contentView.backgroundColor = [UIColor whiteColor];
    self.contentView1 = contentView;
    self.contentView = contentView;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView).offset(0);
        make.top.equalTo(contentView).offset(20);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    titleLabel.font = FONT(15);
    titleLabel.textColor = [UIColor lightGrayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"支付金额";
    
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [contentView addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(contentView).offset(-16);
        make.centerY.equalTo(titleLabel.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [deleteBtn setImage:[UIImage imageNamed:@"guanbi11111"] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];

    
    UILabel *moneyLabel = [[UILabel alloc] init];
    [contentView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(contentView).offset(-10);
        make.top.equalTo(contentView).offset(58);
        make.left.equalTo(contentView).offset(10);
        make.height.mas_equalTo(@30);
    }];
    moneyLabel.text = @"0元";
    moneyLabel.font = B_FONT(28);
    moneyLabel.textColor = [UIColor blackColor];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    self.moneyLabel = moneyLabel;
    
    UILabel *noteLabel = [[UILabel alloc] init];
    [contentView addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(0);
        make.top.equalTo(contentView).offset(107);
        make.right.equalTo(contentView).offset(0);
        make.height.mas_equalTo(@25);
    }];
    noteLabel.font = FONT(11);
    noteLabel.textColor = [UIColor lightGrayColor];
    noteLabel.text = @"  选择支付方式";
    noteLabel.backgroundColor = BY_BACKGROUND_COLOR;
    
    
    ByPayStyleView *balanceView = [[ByPayStyleView alloc] init];
    [contentView addSubview:balanceView];
    [balanceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(0);
        make.right.equalTo(contentView).offset(0);
        make.top.equalTo(noteLabel.mas_bottom).offset(0);
        make.height.mas_equalTo(@50);
    }];
    balanceView.typeView.image = [UIImage imageNamed:@"yuebao"];
    balanceView.titleLabel.text = [NSString stringWithFormat:@"余额支付(可用余额:%.2f元)",BY_USER.user_money.floatValue];
    balanceView.titleLabel.textColor = [UIColor lightGrayColor];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:balanceView.titleLabel.text];
    //设置颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:BY_TEXT_COLOR range:[balanceView.titleLabel.text rangeOfString:@"余额支付"]];
    balanceView.titleLabel.attributedText = attributedString;
    
    [balanceView.selectBtn addTarget:self action:@selector(payStyle:) forControlEvents:UIControlEventTouchUpInside];
    balanceView.selectBtn.tag = 100;
    balanceView.tag = 200;
    balanceView.selectBtn.selected = YES;
    self.type = 0;
    
    NSArray *array  = [ByCacheUser getPayStyle];
    NSLog(@"array = %@", array);
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = [array objectAtIndex:i];
        NSString *pay_type = dic[@"pay_type"];
        ByPayStyleView *wechatView = [[ByPayStyleView alloc] init];
        [contentView addSubview:wechatView];
        [wechatView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(0);
            make.right.equalTo(contentView).offset(0);
            make.top.equalTo(balanceView.mas_bottom).offset(i * 50);
            make.height.mas_equalTo(@50);
        }];
        [wechatView.selectBtn addTarget:self action:@selector(payStyle:) forControlEvents:UIControlEventTouchUpInside];
        wechatView.selectBtn.tag = 101 + i;
        wechatView.tag = 201 + i;
        wechatView.titleLabel.text = dic[@"name"];
        //支付宝
        if ([pay_type isEqualToString:@"FSB0115"] || [pay_type isEqualToString:@"GR_ALIPAY_01"]) {
            wechatView.typeView.image = [UIImage imageNamed:@"支付宝"];
        }
        
        
    }
    
   
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [contentView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(contentView).offset(-38);
        make.left.equalTo(contentView).offset(38);
        make.bottom.equalTo(contentView).offset(-28);
        make.height.mas_equalTo(@40);
    }];
    sureBtn.backgroundColor = BY_THEME_COLOR;
    [sureBtn setTitle:@"确认支付" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FONT(14);
    sureBtn.layer.cornerRadius = 5;
    sureBtn.tag = 200;
    [sureBtn addTarget:self action:@selector(btnAciton:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)setOrderModel:(ByNetModel *)orderModel{
    _orderModel = orderModel;
}



- (void)btnAciton:(UIButton *)btn{
    if (btn.tag == 200) {
        NSArray *array  = [ByCacheUser getPayStyle];
        
        ByNetModel *netModel = [[ByNetModel alloc] init];
        netModel.money = self.orderModel.amount;
        netModel.sn = self.orderModel.sn;
        [ByCoreToolAlert showLoadind:@"支付中" hasMasker:YES];
        
        if (self.type == 0) {
            
            if (self.orderModel.amount.integerValue > BY_USER.user_money.integerValue) {
                [ByCoreToolAlert showOnlyText:@"余额不足，请先充值"];
                return ;
            }
            [ByMoneyApi pay:netModel success:^(ByNetBaseResult *result) {
                [ByCoreToolAlert showSuccessWithText:@"支付成功"];
                ByModelUser *user = BY_USER;
                user.user_money = [NSString stringWithFormat:@"%ld", user.user_money.integerValue - netModel.money.integerValue];
                [ByCacheUser setUserInfo:user];
                if (self.sureBlock){
                    self.sureBlock();
                }
                
            } failure:^(ByNetBaseResult *result) {
                [ByCoreToolAlert showFailureWithText:result.msg];
            }];
            
        }else if (self.type == 1){
            if (array.count > 0) {
                NSDictionary *dic = [array objectAtIndex:0];
               
                NSString *pay_type = dic[@"pay_type"];
                netModel.pay_type = pay_type;
                
                
                [ByMoneyApi fsPayAlipayQrCode:netModel success:^(ByNetBaseResult *result){
                    [ByCoreToolAlert hideHud];
                    
                    if ([netModel.pay_type isEqualToString:@"FSB0115"] || [netModel.pay_type isEqualToString:@"GR_ALIPAY_01"]) {
                        if (self.otherSureBlock) {
                            self.otherSureBlock(result.data[@"data"],1);
                        }
                    }else{
                        if (self.otherSureBlock) {
                            self.otherSureBlock(result.data[@"data"],2);
                        }
                    }
                    
                    
                    
                } failure:^(ByNetBaseResult *result) {
                    [ByCoreToolAlert showFailureWithText:result.msg];
                }];
                
                
                
            }
            

        }else if (self.type == 2){
            if (array.count > 1) {
                
                NSDictionary *dic = [array objectAtIndex:1];
                NSString *pay_type = dic[@"pay_type"];
                netModel.pay_type = pay_type;
                [ByMoneyApi fsPayAlipayQrCode:netModel success:^(ByNetBaseResult *result){
                    [ByCoreToolAlert hideHud];
                    if ([netModel.pay_type isEqualToString:@"FSB0115"] || [netModel.pay_type isEqualToString:@"GR_ALIPAY_01"]) {
                        if (self.otherSureBlock) {
                            self.otherSureBlock(result.data[@"data"],1);
                        }
                    }else{
                        if (self.otherSureBlock) {
                            self.otherSureBlock(result.data[@"data"],2);
                        }
                    }
                    
                } failure:^(ByNetBaseResult *result) {
                    [ByCoreToolAlert showFailureWithText:result.msg];
                }];
            }
            
        }
        
        
    
      
    }
}

- (void)payStyle:(UIButton *)btn{
    
    NSArray *array  = [ByCacheUser getPayStyle];
    if (btn.tag > 100) {
        NSDictionary *dic = [array objectAtIndex:btn.tag - 101];
        NSString *can_use = dic[@"can_use"];
        if (can_use.integerValue != 1) {
            [ByCoreToolAlert showOnlyText:@"暂不支持此支付方式"];
            return;
        }
    }
    
    
    for (int i = 0; i < array.count + 1; i++) {
        ByPayStyleView *styleView = [self.contentView1 viewWithTag:200 + i];
        styleView.selectBtn.selected = NO;
    }
    
    NSInteger index = btn.tag - 100;
    ByPayStyleView *selectsView = [self.contentView1 viewWithTag:index + 200];
    selectsView.selectBtn.selected = YES;
    self.type = index;
    if (self.selectBlock) {
        self.selectBlock(index);
    }
}




- (void)deleteAction:(UIButton *)btn{
    [self remove];
}



@end
